<?php
include_once '../conn.php';

// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 获取表单数据
    $gameId = $_POST["game_id"];
    $title = $_POST["title"];
    $description = $_POST["description"];
    $publisher = $_POST["publisher"];
    $releaseDate = $_POST["release_date"];
    $rating = $_POST["rating"];
    $categoryId = $_POST["category"];

    // 查询原始游戏信息
    $queryGame = "SELECT * FROM games WHERE game_id = $gameId";
    $resultGame = $conn->query($queryGame);

    if ($resultGame->num_rows > 0) {
        $rowGame = $resultGame->fetch_assoc();
        $oldImagePath = $rowGame['image_path'];
    } else {
        echo "<div class='container'>";
        echo "<div class='alert alert-danger' role='alert'>游戏不存在</div>";
        echo "</div>";
        exit(); // 游戏不存在，结束页面显示
    }

    // 处理游戏图片上传
    if (!empty($_FILES["image"]["name"])) {
        $targetDir = "../images/games/";
        $imageFileType = strtolower(pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION));
        $targetFile = $targetDir . uniqid() . '.' . $imageFileType;

        // 删除原始图片
        if (file_exists($oldImagePath)) {
            unlink($oldImagePath);
        }

        if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)) {
            // 修正文件路径为相对路径
            $relativePath = str_replace('..', '.', $targetFile);

            // 更新游戏数据，包括新的图片路径
            $updateQuery = "UPDATE games 
                            SET title = '$title', description = '$description', 
                                publisher = '$publisher', release_date = '$releaseDate', 
                                rating = $rating, category_id = $categoryId, image_path = '$relativePath'
                            WHERE game_id = $gameId";
 
            // 执行更新查询
            if ($conn->query($updateQuery) === TRUE) {
                echo '<script>alert("游戏信息更新成功"); window.location.href = "game.php";</script>';
            } else {
                echo "<div class='container'>";
                echo "<div class='alert alert-danger' role='alert'>Error: " . $updateQuery . "<br>" . $conn->error . "</div>";
                echo "</div>";
            }
        } else {
            echo "<div class='container'>";
            echo "<div class='alert alert-danger' role='alert'>上传新图片失败</div>";
            echo "</div>";
        }
    } else {
        // 用户没有上传新图片，保留原始图片路径
        $updateQuery = "UPDATE games 
                        SET title = '$title', description = '$description', 
                            publisher = '$publisher', release_date = '$releaseDate', 
                            rating = $rating, category_id = $categoryId
                        WHERE game_id = $gameId";

        // 执行更新查询
        if ($conn->query($updateQuery) === TRUE) {
            echo '<script>alert("游戏信息更新成功"); window.location.href = "game.php";</script>';
        } else {
            echo "<div class='container'>";
            echo "<div class='alert alert-danger' role='alert'>Error: " . $updateQuery . "<br>" . $conn->error . "</div>";
            echo "</div>";
        }
    }

    $conn->close();
}
?>
